package com.azhi.srb.core.controller.admin;


import com.azhi.common.exception.Assert;
import com.azhi.common.result.ResponseEnum;
import com.azhi.common.result.Result;
import com.azhi.srb.core.pojo.entity.IntegralGrade;
import com.azhi.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author azhi
 * @since 2025-08-02
 */
@Slf4j
@Api(tags = "积分等级管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public Result list(){
        log.info("这是一个info级别日志");
        log.warn("这是一个warn级别日志");
        log.error("这是一个error级别日志");
        List<IntegralGrade> list = integralGradeService.list();
        return Result.ok().data("list",list).message("获取列表成功");
    }

    @ApiOperation(value="根据id删除积分等级",notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public Result removeById(
            @ApiParam(value = "数据id",example = "100",required = true)
            @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        return Result.ok().data("deleted",b);
    }

    @ApiOperation(value="保存积分等级对象")
    @PostMapping("/save")
    public Result save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){

        //用断言的方式，判断对象是否为空，并抛出异常
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean b = integralGradeService.save(integralGrade);
        if (b){
            return Result.ok().message("保存成功");
        }else {
            return Result.error().message("保存失败");
        }
    }

    @ApiOperation(value="根据id查询积分等级对象")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(value = "数据id",required = true,example = "3")
            @PathVariable Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade!=null){
            return Result.ok().data("integralGrade",integralGrade);
        }else {
            return Result.error().message("查询失败，没获取到等级对象");
        }
    }

    @ApiOperation(value="修改积分等级对象")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        if (b){
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }
    }
}
